<template>
  <div id="app">
    <a-input :value='inputValue' @change='inputValueChange' placeholder="请输入任务" class="my_ipt" />
    <a-button @click='addItem' type="primary">添加事项</a-button>

    <a-list bordered :dataSource="toggleListItem" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox :checked='item.done' @change='e => checkChanged(e, item.id)'>{{item.info}}</a-checkbox>
        <!-- 删除链接 -->
        <a slot="actions" @click='removeItemById(item.id)'>删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <!-- 未完成的任务个数 -->
        <span>{{ undoneList }}条剩余</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button :type="viewKey === 'all' ? 'primary' : 'defalut'" @click='toggleItem("all")'>全部</a-button>
          <a-button :type="viewKey === 'un' ? 'primary' : 'defalut'" @click='toggleItem("un")'>未完成</a-button>
          <a-button :type="viewKey === 'done' ? 'primary' : 'defalut'" @click='toggleItem("done")'>已完成</a-button>
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a @click.prevent='removeAllDone'>清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
  import {
    mapState,
    mapGetters
  } from 'vuex'
  export default {
    name: 'app',
    data() {
      return {}
    },
    methods: {
      // 触发mutations中的方法，修改state中的数据
      // ...mapMutations(['changeValue'], ['addItem']),
      // 监听输入内容改变的事件
      inputValueChange(e) {
        // console.log(e.target.value)
        this.$store.commit('changeValue', e.target.value)
      },
      // 添加事项
      addItem() {
        // 改变state中的数据，需要触发mutations
        this.$store.commit('addItemToList', this.inputValue)
      },
      // 监听复选框是否选中
      checkChanged(e, id) {
        // console.log(e.target.checked, id)
        const params = {
          id: id,
          done: e.target.checked
        }
        this.$store.commit('changeStatus', params)
      },
      // 点击删除item
      removeItemById(id) {
        this.$store.commit('removeListItem', id)
      },
      // 监听删除所有已经完成的事件
      removeAllDone() {
        this.$store.commit('removeAllDoneItem')
      },
      // 切换页面显示的条项
      toggleItem(str) {
        // 通过触发mutations来改变定义在store中的viewKey
        // console.log(str)
        this.$store.commit('displayItemStyle', str)
      }
    },
    created() {
      // this.$store.dispatch('getList')
    },
    computed: {
      ...mapState(['list', 'inputValue', 'viewKey']),
      ...mapGetters(['undoneList', 'toggleListItem'])
    }
  }

</script>

<style scoped>
  #app {
    padding: 10px;
  }

  .my_ipt {
    width: 500px;
    margin-right: 10px;
  }

  .dt_list {
    width: 500px;
    margin-top: 10px;
  }

  .footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

</style>
